clear
insheet using "fromRA/Storting1918_Anna_Menzel6.txt"

drop if valgkrets==.

replace knavn="Kopervik Herred" if knr==1148
replace knavn="Kopervik Ladested" if knr==1105  /* LADESTED MEANS CITY */

sort knavn
merge knavn using dta/knavn3
drop if _merge==2

gen vvotesr2c3=.
gen vvotesr2c4=.

foreach round in 1 2 {
		
		foreach var in svotesr`round'c1 svotesr`round'c2 avotesr`round'c1 avotesr`round'c2 v_avotesr`round'c1 v_avotesr`round'c2 vvotesr`round'c1 vvotesr`round'c2 vvotesr`round'c3 vvotesr`round'c4 lvotesr`round'c1 lvotesr`round'c2 tvotesr`round'c1 tvotesr`round'c2 kirvotesr`round'c1 kirvotesr`round'c2 fvvotesr`round'c1 fvvotesr`round'c2 h_fvvotesr`round'c1 h_fvvotesr`round'c2 hvotesr`round'c1 hvotesr`round'c2 sprvotesr`round'c1 sprvotesr`round'c2 othvotesr`round'c1 othvotesr`round'c2 {
		replace `var'=0 if `var'==.
		}
	gen totalvotes`round'=svotesr`round'c1+svotesr`round'c2+avotesr`round'c1+avotesr`round'c2+v_avotesr`round'c1+v_avotesr`round'c2+vvotesr`round'c1+vvotesr`round'c2+vvotesr`round'c3+vvotesr`round'c4+lvotesr`round'c1+lvotesr`round'c2+tvotesr`round'c1+tvotesr`round'c2+kirvotesr`round'c1+kirvotesr`round'c2+fvvotesr`round'c1+fvvotesr`round'c2+h_fvvotesr`round'c1+h_fvvotesr`round'c2+hvotesr`round'c1+hvotesr`round'c2+sprvotesr`round'c1+sprvotesr`round'c2+othvotesr`round'c1+othvotesr`round'c2
}


foreach round in 1{
	foreach party in s v a v_a l t fv h_fv h oth{
		foreach cand in 1 {
		li knr valgkrets `party'hometownr`round'c`cand' `party'occupationr`round'c`cand' `party'votesr`round'c`cand' if `party'occupationr`round'c`cand'=="" & `party'votesr`round'c`cand'>0
		}
	}
}

foreach round in 2{
	foreach party in s v a l fv h_fv h oth{
		foreach cand in 1 {
		li knr valgkrets `party'hometownr`round'c`cand' `party'occupationr`round'c`cand' `party'votesr`round'c`cand' if `party'occupationr`round'c`cand'=="" & `party'votesr`round'c`cand'>0
		}
	}
}


/* HOMETOWN VARIABLES */


foreach round in 1 2{
	foreach party in s v a v_a l t fv h_fv h oth{
		foreach cand in 1 2 {
		gen match`party'r`round'c`cand'=0
		replace match`party'r`round'c`cand'=1 if `party'hometownr`round'c`cand'==knavn
		replace match`party'r`round'c`cand'=1 if `party'hometownr`round'c`cand'==knavn2
		replace match`party'r`round'c`cand'=1 if `party'hometownr`round'c`cand'==knavn3
		replace match`party'r`round'c`cand'=. if `party'votesr`round'c`cand'==.
		replace match`party'r`round'c`cand'=. if `party'hometownr`round'c`cand'==""
		egen match`party'r`round'c`cand'_di=sum(match`party'r`round'c`cand'), by(valgkrets)
		replace match`party'r`round'c`cand'_di=. if `party'votesr`round'c`cand'==.
		}
	}
}

foreach round in 1 {
	foreach party in v {
			foreach cand in 3 4{
		gen match`party'r`round'c`cand'=0
		replace match`party'r`round'c`cand'=1 if `party'hometownr`round'c`cand'==knavn
		replace match`party'r`round'c`cand'=1 if `party'hometownr`round'c`cand'==knavn2
		replace match`party'r`round'c`cand'=1 if `party'hometownr`round'c`cand'==knavn3
		replace match`party'r`round'c`cand'=. if `party'hometownr`round'c`cand'==""
		replace match`party'r`round'c`cand'=. if `party'votesr`round'c`cand'==.
		egen match`party'r`round'c`cand'_di=sum(match`party'r`round'c`cand'), by(valgkrets)
		replace match`party'r`round'c`cand'_di=. if `party'votesr`round'c`cand'==.
		}
	}
}	

	
sort valgkrets knr	
*br knavn valgkrets matchsr1c1 matchsr1c1_di shometownr1c1	
*br knavn valgkrets matchsr1c1 matchsr1c1_di shometownr1c1 if matchsr1c1_di==0 & shometownr1c1!=" "
*br knavn valgkrets matchvr1c1 matchvr1c1_di vhometownr1c1 if matchvr1c1_di==0 & vhometownr1c1!=""
*br knavn valgkrets matchh_fvr1c1 matchh_fvr1c1_di h_fvhometownr1c1 if matchh_fvr1c1_di==0 & h_fvhometownr1c1!=""

*tab1 *occupationr1c1

gen year=1918
save dta/import1918, replace


/* CREATING DATA SET ON THE VALGKRETS LEVEL */

collapse (sum) *votes* (mean) rejectedelection city, by(valgkrets)

/* CREATING VOTE SHARE VARIABLES */


foreach candidate in svotesr1c1 svotesr1c2 avotesr1c1 avotesr1c2 v_avotesr1c1 v_avotesr1c2 vvotesr1c1 vvotesr1c2 vvotesr1c3 vvotesr1c4 lvotesr1c1 lvotesr1c2 tvotesr1c1 tvotesr1c2 kirvotesr1c1 kirvotesr1c2 fvvotesr1c1 fvvotesr1c2 h_fvvotesr1c1 h_fvvotesr1c2 hvotesr1c1 hvotesr1c2 sprvotesr1c1 sprvotesr1c2 othvotesr1c1 othvotesr1c2 {
gen `candidate'share1=`candidate'/totalvotes1
}

foreach candidate in svotesr2c1 svotesr2c2 avotesr2c1 avotesr2c2 v_avotesr2c1 v_avotesr2c2 vvotesr2c1 vvotesr2c2 vvotesr2c3 vvotesr2c4 lvotesr2c1 lvotesr2c2 tvotesr2c1 tvotesr2c2 kirvotesr2c1 kirvotesr2c2 fvvotesr2c1 fvvotesr2c2 h_fvvotesr2c1 h_fvvotesr2c2 hvotesr2c1 hvotesr2c2 sprvotesr2c1 sprvotesr2c2 othvotesr2c1 othvotesr2c2 {
gen `candidate'share2=`candidate'/totalvotes2
}

foreach round in 1 2 {

	gen maxvotesh`round'=0
	foreach candidate in svotesr`round'c1 svotesr`round'c2 avotesr`round'c1 avotesr`round'c2 v_avotesr`round'c1 v_avotesr`round'c2 vvotesr`round'c1 vvotesr`round'c2 vvotesr`round'c3 vvotesr`round'c4 lvotesr`round'c1 lvotesr`round'c2 tvotesr`round'c1 tvotesr`round'c2 kirvotesr`round'c1 kirvotesr`round'c2 fvvotesr`round'c1 fvvotesr`round'c2 h_fvvotesr`round'c1 h_fvvotesr`round'c2 hvotesr`round'c1 hvotesr`round'c2 sprvotesr`round'c1 sprvotesr`round'c2 othvotesr`round'c1 othvotesr`round'c2 {
	replace maxvotesh`round'=`candidate'share`round' if `candidate'share`round'>maxvotesh`round'
	}
}



gen year=1918

sort valgkrets year
merge valgkrets year using dta/SMD

keep if _merge==3
*scatter approvedvotes1 totalvotes1, mlabel(valgkrets)
*scatter approvedvotes2 totalvotes2, mlabel(valgkrets)

li valgkr if maxvotesh1>0.5 & maxvotesh2!=.
